package com.hot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Solution394 {
    public String decodeString(String s) {
        LinkedList<Integer> numList = new LinkedList<>();
        LinkedList<StringBuffer> strtrList = new LinkedList<>();
        StringBuffer sb = new StringBuffer();
        int m = 0;
        for (char c : s.toCharArray()) {
            if (c>='0'&&c<='9') {
                m = 10 * m + (int)c - '0';
            } else if (c>='a'&&c<='z') sb.append(c);
            else if (c=='[') {
                numList.add(m);
                strtrList.add(sb);
                m = 0;
                sb= new StringBuffer();
            } else {
                int tmpMul = numList.removeLast();
                StringBuffer tmp = new StringBuffer();
                while (tmpMul-->0) tmp.append(sb);
                sb = strtrList.removeLast().append(tmp);
            }
        }
        return sb.toString();
    }
}
